#!/bin/sh

# Slackware build script for: kdissert
# Written by: Michiel van Wessem (BP{k}) <michiel@slackbuilds.org>

set -e

PRGNAM=kdissert
VERSION=1.0.7
ARCH=${ARCH:-i486}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
CWD=$(pwd)
TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}
DOCFILES="AUTHORS COPYING INSTALL README ROADMAP"

# Reset the WAF build System dir to something more saner.
WAF_HOME=$TMP/$PRGNAM-$VERSION/waf-build      
export WAF_HOME

if [ "$ARCH" = "i486" ]; then
    SLKCFLAGS="-O2 -march=i486 -mtune=i686"
elif [ "$ARCH" = "i686" ]; then
    SLKCFLAGS="-O2 -march=i686 -mtune=i686"
fi

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $TMP/$PRGNAM-$VERSION
tar -xjvf $CWD/$PRGNAM-$VERSION.tar.bz2
cd $PRGNAM-$VERSION
chown -R root:root .
chmod -R u+w,go+r-w,a-s .

KDEPREF=$(kde-config --prefix)

CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./waf configure \
  --prefix=$KDEPREF \
  --destdir=$PKG 

./waf build
./waf install --destdir=$PKG

# Clean up the WAF_HOME variable now we don't need it.
unset WAF_HOME

( cd $PKG
find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
)

# Rewrite the kdissert dekstop file so kdissert is placed in Office
sed -i '#Categories=QT;KDE;Utility;X-KDE-More#Categories=QT;KDE;Office;#' $PKG/usr/share/applications/kde/$PRGNAM.desktop

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION 
cp -a $DOCFILES $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild
find $PKG/usr/doc/$PRGNAM-$VERSION -type f -exec chmod 644 {} \;

mkdir $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.tgz
